package com.chixing.controller;

import com.alibaba.fastjson.JSON;
import com.chixing.commons.Result;
import com.chixing.commons.ServerResponse;
import com.chixing.commons.TreeUtil;
import com.chixing.pojo.*;
import com.chixing.service.ICompanyDetailService;
import com.chixing.service.ICompanyManageService;
import com.chixing.service.ISystemMenuService;
import com.chixing.ws.ChatEndpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/backStageLogin")
public class CompanyLoginController {

    @Autowired
    ICompanyManageService companyManageService;
    @Autowired
    ICompanyDetailService companyDetailService;

    @Autowired
    ISystemMenuService iSystemMenuService;

    /**
     *登录验证
     */
    @RequestMapping("check")
    @ResponseBody
    public Result checkLogin(String username, String password, HttpSession session){
        CompanyManage companyManage = companyManageService.checkLogin(username, password);
        CompanyDetail companyDetail = companyDetailService.selectById(companyManage.getCompanyId());
        if(companyManage!=null) {
            session.setAttribute("loginuser", companyManage);
            session.setAttribute("loginManage", companyManage);
            session.setAttribute("companyDetail", companyDetail);
            return new Result(20001, "登录成功", companyManage);
        }
        return new Result(20000,"登录失败",companyManage);
    }

    /**
     * 初始化菜单接口
     */
    @RequestMapping("/loadMenuList")
    @ResponseBody
    public String loadMenuList(){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> homeInfo = new HashMap<>();
        Map<String,Object> logoInfo = new HashMap<>();
        List<SystemMenu> systemMenus = iSystemMenuService.selectAll();
        List<MenuVo> menuVos = new LinkedList<>();
        for (SystemMenu systemMenu:systemMenus) {
            MenuVo menuVo = new MenuVo();
            menuVo.setHref(systemMenu.getHref());
            menuVo.setIcon(systemMenu.getIcon());
            menuVo.setId(systemMenu.getId());
            menuVo.setSpread(systemMenu.getSpread());
            menuVo.setTarget(systemMenu.getTarget());
            menuVo.setTitle(systemMenu.getTitle());
            menuVo.setPid(systemMenu.getPid());
            menuVos.add(menuVo);
        }
        menuVos.forEach(System.out::println);
        homeInfo.put("title","首页");
        homeInfo.put("href","backStageWelcome.jsp");
        logoInfo.put("title","link-chat");
        logoInfo.put("image","../static/layui/images/logo.png");
        logoInfo.put("href","");

        map.put("menuInfo", TreeUtil.toTree(menuVos,0));
        map.put("homeInfo", homeInfo);
        map.put("logoInfo", logoInfo);

        return JSON.toJSONString(map);
    }

    @RequestMapping("/getUsername")
    @ResponseBody
    public CompanyManage getUsername(HttpSession session) {
        CompanyManage companyManage = (CompanyManage) session.getAttribute("loginManage");
        return companyManage;
    }

//    @RequestMapping("/getAllUsername")
//    public ServerResponse getAllUsername() {
//        List<String> allUsername = personCenterService.getAllUsername();
//        return ServerResponse.success("成功", allUsername);
//    }
//
//    @RequestMapping("/isOnline")
//    public ServerResponse isOnline() {
//        Map<String, ChatEndpoint> onlineUsersName = ChatEndpoint.onlineUsersName;
//        Set<String> names = onlineUsersName.keySet();
//        List<String> allOnlineName = new ArrayList<>();
//        allOnlineName.addAll(names);
//        return ServerResponse.success("成功", allOnlineName);
//    }

}
